/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-23
 * Time: 13:53
 */
public class SingleLinkedList {


    static class Node {
        //结点类
        public int val;//数据域
        public Node next;//下一个结点

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;//头结点


    //头插法
    public void addFirst(int data) {
        Node newNode = new Node(data);
        //如果没有元素,直接添加
        if (this.head == null) {
            this.head = newNode;
            return;
        }
        newNode.next = this.head;
        this.head = newNode;
    }

    //尾插法
    public void addLast(int data) {

        Node newNode = new Node(data);
        //如果没有元素,直接插
        if (this.head == null) {
            this.head = newNode;
            return;
        }

        //先找尾结点
        Node tail = head;
        while (tail.next != null) {
            tail = tail.next;
        }
        //找到尾了,插入
        tail.next = newNode;

    }

    //任意位置插入,第一个数据节点为 0号下标
    public void addIndex(int index, int data) {
        //判断index位置的合法性
        try {
            if (index < 0 || index > size()) {
                throw new IndexNotLegalException("index不合法");
            }
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        Node newNode = new Node(data);
        //找到index下标前一个位置
        Node pre = head;
        for (int i = 0; i < index - 1; i++) {
            pre = pre.next;
        }
        //开始交换
        newNode.next = pre.next;
        pre.next = newNode;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            return;
        }
        Node pre = null;
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                break;
            }
            pre = cur;
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        Node pre = head;
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        while (true) {
            assert head != null;
            if (!(head.val == key)) break;
            head = head.next;
            if (head == null) {
                break;
            }
        }
    }

    //得到单链表的长度
    public int size() {
        int size = 0;
        Node cur = this.head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //清空链表
    public void clear() {
        this.head = null;
    }
}